package com.example.project.dao;

import com.example.project.pojo.ParkingType;
import com.example.project.util.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ParkingTypeDAO {
    public void addPark(ParkingType parkingType) {

        Connection connection = DBHelper.getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "insert into parking_type(type_name,money) value(?,?)";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, parkingType.getTypeName());
            preparedStatement.setDouble(2, parkingType.getMoney());

            preparedStatement.executeUpdate();
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            DBHelper.close(preparedStatement, connection);
        }
    }

    public List<ParkingType> findAll() {
        List<ParkingType> List = new ArrayList<>();

        Connection connection = DBHelper.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select * from parking_type";
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                ParkingType parkingType = new ParkingType();
                parkingType.setId(resultSet.getInt("id"));
                parkingType.setTypeName(resultSet.getString("type_name"));
                parkingType.setMoney(resultSet.getDouble("money"));
                List.add(parkingType);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.close(resultSet, preparedStatement, connection);
        }

        return List;
    }

    public void delPark(int id) {
        Connection conn = DBHelper.getConnection();

        PreparedStatement preparedStatement = null;

        try {

            String sql = "delete from parking_type where id=?";
            preparedStatement = conn.prepareStatement(sql);

            //设置?的值
            preparedStatement.setInt(1, id);

            preparedStatement.executeUpdate();


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.close(preparedStatement, conn);
        }
    }

    public ParkingType viewUpParkingType(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        connection = DBHelper.getConnection();

        String sql = "select * from parking_type where id=?";
        ParkingType parkingType = new ParkingType();
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);

            resultSet = preparedStatement.executeQuery();

          while(resultSet.next()) {
              parkingType.setId(resultSet.getInt("id"));
              parkingType.setTypeName(resultSet.getString("type_name"));
              parkingType.setMoney(resultSet.getDouble("money"));
          }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            DBHelper.close(resultSet, preparedStatement, connection);
        }

        return parkingType;

    }

    public void updateParkingType(ParkingType parkingType) {
        Connection connection = DBHelper.getConnection();
        PreparedStatement preparedStatement = null;

        String sql = "UPDATE parking_type SET type_name=?,money=? WHERE  id=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, parkingType.getTypeName());

            preparedStatement.setDouble(2, parkingType.getMoney());
            preparedStatement.setInt(3, parkingType.getId());
            preparedStatement.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBHelper.close(preparedStatement, connection);
        }
    }


}
